﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class GateSetForm : Form
    {
        public GateSetForm()
        {
            InitializeComponent();
        }

        private void GateSetForm_Load(object sender, EventArgs e)
        {
            int gateWay = int.Parse(ConfigurationManager.AppSettings["GateWay"]);
            this.chkGateWayList.SelectedIndex = gateWay;
            int gateControlWay = int.Parse(ConfigurationManager.AppSettings["GateControlWay"]);
            this.chkGateContolWayList.SelectedIndex = gateControlWay;
            this.txtLeftIP.Text = ConfigurationManager.AppSettings["IpOfLeftGateIO"].ToString();
            this.txtLeftFrontUpPoint.Text = ConfigurationManager.AppSettings["LeftFGateUp"].ToString();
            this.txtLeftFrontDownPoint.Text = ConfigurationManager.AppSettings["LeftFGateDown"].ToString();
            this.txtLeftBackUpPoint.Text = ConfigurationManager.AppSettings["LeftBGateUp"].ToString();
            this.txtLeftBackDownPoint.Text = ConfigurationManager.AppSettings["LeftBGateDown"].ToString();

            this.txtRightIP.Text = ConfigurationManager.AppSettings["IpOfRightGateIO"].ToString();
            this.txtRightFrontUpPoint.Text = ConfigurationManager.AppSettings["RightFGateUp"].ToString();
            this.txtRightFrontDownPoint.Text = ConfigurationManager.AppSettings["RightFGateDown"].ToString();
            this.txtRightBackUpPoint.Text = ConfigurationManager.AppSettings["RightBGateUp"].ToString();
            this.txtRightBackDownPoint.Text = ConfigurationManager.AppSettings["RightBGateDown"].ToString();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["GateWay"].Value =this.chkGateWayList.SelectedIndex.ToString();
            cfa.AppSettings.Settings["GateControlWay"].Value = this.chkGateContolWayList.SelectedIndex.ToString();

            cfa.AppSettings.Settings["IpOfLeftGateIO"].Value = this.txtLeftIP.Text.Trim();
            cfa.AppSettings.Settings["LeftFGateUp"].Value = this.txtLeftFrontUpPoint.Text.Trim();
            cfa.AppSettings.Settings["LeftFGateDown"].Value = this.txtLeftFrontDownPoint.Text.Trim();
            cfa.AppSettings.Settings["LeftBGateUp"].Value = this.txtLeftBackUpPoint.Text.Trim();
            cfa.AppSettings.Settings["LeftBGateDown"].Value = this.txtLeftBackDownPoint.Text.Trim();

            cfa.AppSettings.Settings["IpOfRightGateIO"].Value = this.txtRightIP.Text.Trim();
            cfa.AppSettings.Settings["RightFGateUp"].Value = this.txtRightFrontUpPoint.Text.Trim();
            cfa.AppSettings.Settings["RightFGateDown"].Value = this.txtRightFrontDownPoint.Text.Trim();
            cfa.AppSettings.Settings["RightBGateUp"].Value = this.txtRightBackUpPoint.Text.Trim();
            cfa.AppSettings.Settings["RightBGateDown"].Value = this.txtRightBackDownPoint.Text.Trim();

            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
